//
//  BanBu_SexSelectView.m
// Halfarea
//
//  Created by banbu3 on 14-2-13.
//
//

#import "BanBu_SexSelectView.h"

@implementation BanBu_SexSelectView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.selectedIndex = -1;
//        self.frame = CGRectMake(0, 0, 240, 50);
//        self.userInteractionEnabled = YES;
        UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250, 50)];
        backView.image = [UIImage imageNamed:@"sexSelectBV.png"];
        [self addSubview:backView];
        backView.userInteractionEnabled = YES;
        [backView release];
        //            NSArray *items = [NSArray arrayWithObjects:[NSString stringWithFormat:@" ♂   %@   ",NSLocalizedString(@"boy",nil)], [NSString stringWithFormat:@" ♀   %@   ",NSLocalizedString(@"girl",nil)],nil];
        
        
        UIButton *mbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        mbtn.frame = CGRectMake(5, 5, 115, 40);
        [mbtn setTitle:[NSString stringWithFormat:@" ♂   %@   ",NSLocalizedString(@"boy",nil)] forState:UIControlStateNormal];
        mbtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        
        [mbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//        mbtn setTitleColor:[UIColor c] forState:<#(UIControlState)#>
//        mbtn.backgroundColor = [UIColor redColor];
        [mbtn addTarget:self action:@selector(sexButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        self.mButton = mbtn;
        mbtn.tag = 1;
        [backView addSubview:mbtn];
        
        UIButton *fbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        fbtn.frame = CGRectMake(130, 5, 115, 40);
        fbtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [fbtn setTitle:[NSString stringWithFormat:@" ♀   %@   ",NSLocalizedString(@"girl",nil)] forState:UIControlStateNormal];
        
        [fbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [fbtn addTarget:self action:@selector(sexButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        self.fButton = fbtn;
        fbtn.tag = 2;
        [backView addSubview:fbtn];
        
        
        
        
        
    }
    return self;
}
-(void)sexButtonPressed:(UIButton *)btn
{
    [self setSelectedIndex:btn.tag-1];
}
-(void)setButtonSelectIndex:(NSInteger)index
{
//    NSArray *arr = @[[UIImage imageNamed:@"startverify.png"],[UIImage imageNamed:@"sexSelectButton"]];
    if(_selectedIndex == index)
    {
        return;
    }
    
    if(!index)
    {
        [self.mButton setBackgroundImage:[UIImage imageNamed:@"sexSelected.png"]  forState:UIControlStateNormal];
        [self.fButton setBackgroundImage:nil forState:UIControlStateNormal];
        
        [self.mButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
         [self.fButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    else if(index == 1)
    {
        [self.fButton setBackgroundImage:[UIImage imageNamed:@"sexSelected.png"] forState:UIControlStateNormal];
        [self.mButton setBackgroundImage:nil forState:UIControlStateNormal];
        [self.fButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.mButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    else
    {
        [self.fButton setBackgroundImage:nil forState:UIControlStateNormal];
        [self.mButton setBackgroundImage:nil forState:UIControlStateNormal];
        [self.fButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.mButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
//    [_delegate sexValueDidChange:index];
    _selectedIndex = index;
}
-(void)setSelectedIndex:(NSInteger)selectedIndex
{
    if(_selectedIndex == selectedIndex)
    {
        return;
    }
    
    
    [self setButtonSelectIndex:selectedIndex];
    [_delegate sexValueDidChange:selectedIndex];
}


@end
